package com.wswy.wzcx.ui

import android.os.Bundle
import com.blankj.utilcode.util.SPUtils
import com.che.libcommon.utils.ToastUtils
import com.umeng.message.PushAgent
import com.wswy.wzcx.R
import com.wswy.wzcx.model.DataCenter
import com.wswy.wzcx.model.home.RouterUtils
import com.wswy.wzcx.module.ChannelManager
import com.wswy.wzcx.module.DebugConfig
import com.wswy.wzcx.module.GlobalConfigManager
import com.wswy.wzcx.module.ad.AdConfigLoader
import com.wswy.wzcx.module.base.CBaseActivity
import com.wswy.wzcx.utils.Tools
import kotlinx.android.synthetic.main.activity_test.*


class TestActivity : CBaseActivity() {

    companion object {
        private const val TAG = "TestActivity"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)

        setAppTitle("测试")

        tv_app_info.text  = packageManager.getPackageInfo(packageName,0)?.run {

            "包名:${packageName}---${versionCode}---${versionName}\n渠道:${ChannelManager.getChannel()}"
        }
        if (DataCenter.get().hasLogin()){
            DataCenter.get().userMode?.let {
                tv_app_info.append("\n已登录\n手机号:${it.mobile}\nuid:${it.userAlias}")
            }
        }else{
            tv_app_info.append("\n未登录")
        }

        tv_app_info.append("\nPushId:${PushAgent.getInstance(applicationContext).registrationId}")

        tv_app_info.append("\n广告位\n")
        tv_app_info.append("csj-> ${AdConfigLoader.csjConfig}\n")
        tv_app_info.append("gdt-> ${AdConfigLoader.gdtConfig}\n")

        updateConfig(GlobalConfigManager.getInstance().debugConfig)

        switch_force_http.setOnCheckedChangeListener { _, isChecked ->

            GlobalConfigManager.getInstance().debugConfig.forceHttp = isChecked


            SPUtils.getInstance("_wzc_t").put("debug_forceHttp",isChecked)

        }

        switch_open_log.setOnCheckedChangeListener { _, isChecked ->
            GlobalConfigManager.getInstance().debugConfig.openLog = isChecked
            SPUtils.getInstance("_wzc_t").put("debug_openLog",isChecked)
        }


        rl_debug_x5.setOnClickListener {
            RouterUtils.startTarget(this,RouterUtils.TYPE_WEB,"http://debugtbs.qq.com")
        }

        rl_clear_config.setOnClickListener {
            try {
                Tools.getApiCache().clear()
                Tools.getCache().clear()
                SPUtils.getInstance().clear()
                ToastUtils.showText("清除成功")
            } catch (e: Exception) {
                ToastUtils.showText("错误--${e.message}")
            }

        }
    }

    private fun updateConfig(debugConfig: DebugConfig){

        switch_force_http.isChecked = debugConfig.forceHttp
        switch_open_log.isChecked = debugConfig.openLog
    }

}